package com.waitingsnow.modules.shell.view.video
{
import assets.shellView.VideoEffectAsset;

import com.waitingsnow.modules.shell.view.video.events.VideoEffectEvent;

import fl.controls.Button;
import fl.controls.Slider;
import fl.events.SliderEvent;

import flash.display.Sprite;
import flash.events.MouseEvent;

/**
 * 
 * @author waitingsnow
 * $Id: VideoEffect.as 2 2012-05-17 10:10:03Z cgslendy@gmail.com $
 */
public class VideoEffect extends Sprite
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	/**
	 * Constructs a new <code>VideoEffect</code> instance.
	 * 
	 */
	public function VideoEffect()
	{
		super();
		initDisplay();
	}
	
	//==========================================================================
	//  Variables
	//==========================================================================
	private var videoEffectAsset:VideoEffectAsset;
	private var brightness_sl:Slider;
	private var contrast_sl:Slider;
	private var saturation_sl:Slider;
	private var hue_sl:Slider;
	private var ok_btn:Button;
	private var cancel_btn:Button;
	private var default_btn:Button;
	
	//==========================================================================
	//  Properties
	//==========================================================================
	private var _brightness:Number;
	public function get brightness():Number
	{
		return brightness_sl.value/10;
	}

	private var _contrast:Number;
	public function get contrast():Number
	{
		return contrast_sl.value/10;
	}

	private var _saturation:Number;
	public function get saturation():Number
	{
		return saturation_sl.value/10;
	}

	private var _hue:Number;
	public function get hue():Number
	{
		return hue_sl.value/10;
	}

	//==========================================================================
	//  Methods
	//==========================================================================
	private function initDisplay():void
	{
		videoEffectAsset = new VideoEffectAsset();
		addChild(videoEffectAsset);
		brightness_sl = createSlider(110, 48, -1, 3, 1);
		contrast_sl = createSlider(110, 78, -1, 3, 1);
		saturation_sl = createSlider(110, 108, -1, 3, 1);
		hue_sl = createSlider(110, 138, 0, 36, 0);
		brightness_sl.addEventListener(SliderEvent.THUMB_DRAG, changeHandler);
		contrast_sl.addEventListener(SliderEvent.THUMB_DRAG, changeHandler);
		saturation_sl.addEventListener(SliderEvent.THUMB_DRAG, changeHandler);
		hue_sl.addEventListener(SliderEvent.THUMB_DRAG, changeHandler);
		
		default_btn = createBtn("恢复默认", 220, 150, 80);
		ok_btn = createBtn("确定", 90, 195);
		cancel_btn = createBtn("取消", 180, 195);
		default_btn.addEventListener(MouseEvent.CLICK, defaultBtnClickHandler);
		ok_btn.addEventListener(MouseEvent.CLICK, okBtnClickHandler);
		cancel_btn.addEventListener(MouseEvent.CLICK, cancelBtnClickHandler);
		videoEffectAsset.close_btn.addEventListener(MouseEvent.CLICK, cancelBtnClickHandler);
		
	}
	
	private function createSlider(x:int, y:int, min:Number, max:Number, val:Number, w:int = 140):Slider
	{
		var sl:Slider = new Slider();
		sl.x = x;
		sl.y = y;
		sl.minimum = min*10;
		sl.maximum = max*10;
		sl.value = val*10;
		sl.width = w;
		videoEffectAsset.addChild(sl);
		return sl;
	}
	
	private function createBtn(label:String, x:int, y:int, w:int = 50):Button
	{
		var btn:Button = new Button();
		btn.label = label;
		btn.x = x;
		btn.y = y;
		btn.width = w;
		videoEffectAsset.addChild(btn);
		return btn;
	}
	
	private function dispose():void
	{
		parent && parent.removeChild(this);
	}
	
	private function showDefault():void
	{
		brightness_sl.value = 10;
		contrast_sl.value = 10;
		saturation_sl.value = 10;
		hue_sl.value = 0;
		dispatchEvent(new VideoEffectEvent(VideoEffectEvent.COLOR_CHANGE));
	}
	
	//==========================================================================
	//  Event Handlers
	//==========================================================================
	private function changeHandler(event:SliderEvent):void
	{
		dispatchEvent(new VideoEffectEvent(VideoEffectEvent.COLOR_CHANGE));
	}
	
	private function defaultBtnClickHandler(event:MouseEvent):void
	{
		showDefault();
	}
		
	private function okBtnClickHandler(event:MouseEvent):void
	{
		dispose();
		dispatchEvent(new VideoEffectEvent(VideoEffectEvent.COLOR_CHANGE));
	}
		
	private function cancelBtnClickHandler(event:MouseEvent):void
	{
		dispose();
		showDefault();
	}
		
}
}